import java.awt.Dimension;
import java.awt.Toolkit;

import globals.Global;
import javax.swing.JFrame;

/**
 * @author Ben Elliott
 * 
 * The main entry point of the game program.  This creates the game-loop
 * to continuously run the game.
 */
public class Main extends JFrame {
	private static final long serialVersionUID = 98621321532L;

	Game game;
	
	class GameThread extends Thread {
		public GameThread(String str) {
			super(str);
		}
		public void run() {
			// Game is started
			Global.Instance().gameRunning = true;
			
			// Setup time variables
			long currentTime = System.nanoTime();
			long lastTime = System.nanoTime();
			long elapsed = 0;
			
			System.out.println("Thread Start...");
			
			// Game is already loaded from the visibility set to true due to the initial draw call
			//game.Load();
			
			// Main Game Loop
			int fps = 0;
			long lastFpsTime = 0;
			while (Global.Instance().gameRunning) {
				// Update elapsed time
				lastTime = currentTime;
				currentTime = System.nanoTime();
				elapsed = currentTime - lastTime;
				
				// Update the game
				game.Update(elapsed/1000000);  // pass milliseconds
				
				// Draw the game
				game.repaint();
				
				// Count frames
				lastFpsTime += elapsed;
				fps++;
				if (lastFpsTime > 1000000000) {
					Global.Instance().fps = fps;
					lastFpsTime = 0;
					fps = 0;
				}
				
				// Short sleep
				try {
					// Nanoseconds a frame should take
					long frameTime = 1000000000 / 60;
					
					// Time that this frame should have taken but did not
					// Converted into milliseconds
					long extraTime = Math.round((currentTime - System.nanoTime() + frameTime)/1000000d);
					
					// Sleep if needed
					if (extraTime > 0)
						Thread.sleep( extraTime );
					else
						System.out.println("delay: " + extraTime);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			System.out.println("Thread End...");
			System.exit(0);
		}
	}
	
	public Main() {
		// TODO: options
		boolean fullscreenMode = false;
		
		// Show the game
		game = new Game();
        setTitle("Aurora");
        add(game);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        
        // Enable FullScreen
        if (fullscreenMode) {
        	setUndecorated(true);
        	setBounds(0,0,screenSize.width, screenSize.height);
        	Global.Instance().winWidth = screenSize.width;
        	Global.Instance().winHeight = screenSize.height;
        } else {
        	setBounds(
        			screenSize.width/2 - Global.Instance().winWidth/2,
        			screenSize.height/2 - Global.Instance().winHeight/2,
        			Global.Instance().winWidth, Global.Instance().winHeight);
        }
        setVisible(true);
        setResizable(false);
        
        
        // Start the Game Thread
        GameThread gameThread = new GameThread("Game");
        gameThread.start();
	}
	
	public static void main(String[] args) {
		new Main();
	}
}
